package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.service.IUserFollowsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户关注/粉丝 业务层
 *
 * @author Wangyk
 * @date 2019/1/14 11:15
 */
@Controller
public class UserFollowsServiceImpl implements IUserFollowsService {
    @Autowired
    private UserMapper userMapper;


    /**
     * 添加一个关注关系
     *
     * @param masterUserId 被关注者
     * @param slaveUserId  关注者
     */
    @Override
    public void addOrUpdateAFollow(Long masterUserId, Long slaveUserId) {
        //1:先查两个用户之间有没有关注关系
        Map userFollowRelation = userMapper.selectAFollowRelationById(masterUserId, slaveUserId);
        //2:如果用户没有关注/已经取消关注就添加一条关注, 如果正在关注 就添加取消关注
        if ((userFollowRelation == null) || userFollowRelation.get("state").equals(0)) {
            userMapper.insertAFollowRelation(masterUserId, slaveUserId, true);
        } else {
            userMapper.updateAFollowRelation(masterUserId, slaveUserId, false);
        }
    }

    @Override
    public HashMap<String, Object> getFollowInfoByUserId(Long masterUserId, Long slaveUserId) {
        HashMap<String, Object> followInfoMap = new HashMap<>();
        //1:关注/粉丝数
        followInfoMap.put("followNums", userMapper.selectFollowNumsByUserId(masterUserId));
        followInfoMap.put("fansNums", userMapper.selectFansNumsByUserId(masterUserId));
        //2:是否有关注关系
        Map userFollowRelation = userMapper.selectAFollowRelationById(masterUserId, slaveUserId);
        if ((userFollowRelation != null) && userFollowRelation.get("state").equals(1)) {
            followInfoMap.put("isFollowed", true);
        } else {
            followInfoMap.put("isFollowed", false);
        }


        return followInfoMap;
    }

    @Override
    public Map<String, List<User>> getAUserzFollowersFans(Long userId) {
        HashMap<String, List<User>> followersFansListMap = new HashMap<>();
        List<User> fansList = userMapper.selectUserzFansByUserId(userId);
        List<User> followersList = userMapper.selectUserzFollowListByUserId(userId);

        followersFansListMap.put("fansList", fansList);
        followersFansListMap.put("followersList", followersList);
        return followersFansListMap;
    }
}
